package com.wfm.threadlocal;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTest {

    public void threadLocalFunc(){
        String name = Thread.currentThread().getName();
        ThreadLocal<Integer> local = ThreadLocal.withInitial(()->10);
        System.out.println(name+"local:"+local.get());
        for (int i = 0; i < 10000; i++) {
            local.set(local.get()+1);
        }
        System.out.println(name+"local:"+local.get());
    }

    public void localFunc(){
        String name = Thread.currentThread().getName();
        int local = 10;
        System.out.println(name+"local:"+local);
        for (int i = 0; i < 10000; i++) {
            local=local+1;
        }
        System.out.println(name+"local:"+local);
    }

    public void visit(){
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.execute(()->{
            threadLocalFunc();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
